package com.ch.engine.biz.vims;

import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 设置换店历史
 *
 * @author wangchunhui
 * @date 2025/1/15 11:49
 */
public class ChangeHistorySetter {

    public static void setChangeHistoryList(List<ShopChangeDetails> shopChangeDetailsList, List<ProjectMessageDTO> projectMessageList) {
        // 创建一个 Map，方便通过 investOrderNo 快速找到对应的 ProjectMessageDTO
        Map<String, ProjectMessageDTO> projectMessageMap = projectMessageList.stream()
                .collect(Collectors.toMap(ProjectMessageDTO::getInvestOrderNo, projectMessage -> projectMessage));

        // 创建一个 Map，方便通过新合约编号快速查找对应的换店记录
        Map<String, List<ShopChangeDetails>> detailsMap = shopChangeDetailsList.stream().collect(Collectors.groupingBy(ShopChangeDetails::getChangeInvestOrderNo));

        // 遍历每一个 ProjectMessageDTO
        for (ProjectMessageDTO projectMessage : projectMessageList) {
            String currentInvestOrderNo = projectMessage.getInvestOrderNo();
            List<ChangeHistoryDTO> changeHistoryList = new ArrayList<>();

            // 使用一个栈来存储需要处理的合约编号
            LinkedList<String> stack = new LinkedList<>();
            stack.push(currentInvestOrderNo);

            while (!stack.isEmpty()) {
                // 取出当前处理的合约编号
                String investOrderNo = stack.pop();
                if (detailsMap.containsKey(investOrderNo)) {
                    // 获取与当前合约编号相关的所有换店记录
                    List<ShopChangeDetails> relatedDetails = detailsMap.get(investOrderNo);
                    for (ShopChangeDetails detail : relatedDetails) {
                        // 创建并设置 ChangeHistoryDTO
                        ChangeHistoryDTO history = new ChangeHistoryDTO();
                        history.setChangeNo(detail.getChangeNo());
                        history.setInvestOrderNo(detail.getOriginInvestOrderNo());
                        history.setBaseRatio(detail.getBaseRatio());

                        // 从 projectMessageMap 中找到对应的 startDate 和 endDate
                        ProjectMessageDTO relatedMessage = projectMessageMap.get(detail.getOriginInvestOrderNo());
                        if (relatedMessage != null) {
                            history.setStartDate(relatedMessage.getStartDate());
                            history.setEndDate(relatedMessage.getEndDate());
                        }

                        changeHistoryList.add(history);

                        // 将原合约编号加入栈中，继续处理它的换店记录
                        stack.push(detail.getOriginInvestOrderNo());
                    }
                }
            }
            // 设置最终的换店历史记录到 ProjectMessageDTO
            projectMessage.setChangeHistoryList(changeHistoryList);
        }
    }

    public static void main(String[] args) {
        List<ShopChangeDetails> shopChangeDetailsList = new ArrayList<>();
        shopChangeDetailsList.add(new ShopChangeDetails("1", "A", "B", new BigDecimal("0.2")));
        shopChangeDetailsList.add(new ShopChangeDetails("1", "A", "C", new BigDecimal("0.3")));
        shopChangeDetailsList.add(new ShopChangeDetails("1", "A", "E", new BigDecimal("0.5")));
        shopChangeDetailsList.add(new ShopChangeDetails("2", "B", "D", new BigDecimal("1")));
        shopChangeDetailsList.add(new ShopChangeDetails("2", "C", "D", new BigDecimal("1")));

        List<ProjectMessageDTO> projectMessageList = new ArrayList<>();
        projectMessageList.add(new ProjectMessageDTO("A", LocalDate.of(2024, 1, 1), LocalDate.of(2024, 1, 31), null));
        projectMessageList.add(new ProjectMessageDTO("B", LocalDate.of(2024, 2, 1), LocalDate.of(2024, 2, 29), null));
        projectMessageList.add(new ProjectMessageDTO("C", LocalDate.of(2024, 2, 1), LocalDate.of(2024, 2, 29), null));
        projectMessageList.add(new ProjectMessageDTO("E", LocalDate.of(2024, 2, 1), LocalDate.of(2024, 2, 29), null));
        projectMessageList.add(new ProjectMessageDTO("D", LocalDate.of(2024, 3, 1), LocalDate.of(2024, 3, 31), null));

        setChangeHistoryList(shopChangeDetailsList, projectMessageList);

        System.out.println(JSONObject.toJSONString(projectMessageList));
    }
}
